import { AIPropertyDescriptor, IField } from "../AIProperty";
import { Field } from "./Field";

export class EnumField extends Field implements IField {
    create(o: AIPropertyDescriptor, data: any) {
        let d = o.enumSource[0];
        if ('object' == typeof d) {
            d = d.value;
        }

        let p = this._create(o, 'Enum', data, d);
        let val = p.getChild("value") as gui.ComboBox;
        let enumSource = o.enumSource;
        let values: string[] = [];
        let items: string[] = [];
        let selectIndex: number;
        for (let i = enumSource.length - 1; i >= 0; i--) {
            let d = enumSource[i];
            if ('string' == typeof d || 'number' == typeof d) {
                values.unshift(d as any);
                items.unshift(String(d));
                if (d == o.default) {
                    selectIndex = i;
                }
            } else {
                values.unshift(d.value);
                items.unshift(d.name);
                if (o.default == d.value) {
                    selectIndex = i;
                }
            }
        }
        val.values = values;
        val.items = items;
        if (null != selectIndex)
            val.selectedIndex = selectIndex;
        return p;
    }
    protected _setValue(val: any): void {

    }
    protected _changeHandle(e: gui.Event) {
        let txt = (e.target as any).value;
        this.data[this.o.name] = txt;
        this._checkChange();
    }

}